package edu.mtu.citizenscience.cs.notepad.activity.noteslist.note;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import edu.mtu.citizenscience.cs.notepad.NPActivity;
import edu.mtu.citizenscience.cs.notepad.activity.R;
import edu.mtu.citizenscience.cs.notepad.activity.noteslist.note.photo.PhotoActivity;
import edu.mtu.citizenscience.cs.notepad.activity.noteslist.note.photo.PhotoGridAdapter;
import edu.mtu.citizenscience.cs.notepad.model.ModelNote;
import edu.mtu.citizenscience.cs.notepad.model.note.ModelImage;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Photos Activity Responsible for displaying, adding
 * and removing photos from the note.
 * 
 * Displays all note images in a grid
 */
public class PhotosActivity extends NPActivity {
	// The model of the note the photos get attached to
	private ModelNote note;
	
	/**
	 * @since 1.0
	 * 
	 * Handles when one of the photos on the photo grid is clicked
	 */
	private class PhotoClickHander implements OnItemClickListener {
		
		/**
		 * Called by the CitsciGPS when it has a location
		 * 
		 * @param parent - AdapterView<?> - The AdapterView where the click happened.
		 * @param view - View - The view within the AdapterView that was clicked (this will be a view provided by the adapter)
		 * @param position - int - The position of the view in the adapter.
		 * @param id - long - The row id of the item that was clicked.
		 * @return void
		 */
		@Override
    	public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
    	{
			// get the id of the note image clicked on the grid
			long imageId = note.getImages().get(position).getId();
			
			// launch the Photo Activity passing the id of the current note and the id of the clicked image
			// to view or delete the photo
			startActivity(new Intent(aq.getContext(), PhotoActivity.class).putExtra(ModelNote.NOTE_ID, note.getId()).putExtra(ModelImage.IMAGE_ID, imageId));
    	}
    	
    }
	
	/**
	 * Photos Activity creation handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.activity_photos);
		
		// try to get the note id from the intent bundle
		long noteId = this.getIntent().getLongExtra(ModelNote.NOTE_ID, -1);
		
		// check to see if got a note id
		if (noteId > -1)
		{
			// set the note model to the model with the given note id
			this.note = new ModelNote(this, noteId).find();
		}
		else
		{
			// no note to attach the photos to so finish the activity
			this.finish();
		}
		
		// set click handler on the image attachment button to addImageClick
		this.aq.id(R.id.attachmentImageButton).clicked(this, "addImageClick");
	}
	
	/**
	 * Photos Activity resume handler
	 * 
	 * @param   void
	 * @return	void
	 */
	@Override
	public void onResume()
	{
		super.onResume();
		
		// reload the note model from the DB incase an image has been added or removed
		this.note = new ModelNote(this, this.note.getId()).find();
		
		// check if note has any attached images
		if (this.note.getImages().size() > 0)
		{
			// get the note grid
			GridView gridview = (GridView) findViewById(R.id.photoGrid);
			// set the grid adapter to a new photo list adapter loaded with the note's images
		    gridview.setAdapter(new PhotoGridAdapter(this.aq, this.note.getImages()));
		    // set the grids item click listener to the photo click handler
		    gridview.setOnItemClickListener(new PhotoClickHander());
		}

	}
	
	/**
	 * onClick listener for new photo button
	 * 
	 * @param   void
	 * @return	void
	 */
	public void addImageClick()
	{
		// launch the Photo Activity passing the note id to add a new photo
		this.startActivity(new Intent(this, PhotoActivity.class).putExtra(ModelNote.NOTE_ID, this.note.getId()));
	}
}